package cqie.personal.frame.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cqie.personal.frame.bo.AddModifyBO;
import cqie.personal.frame.bo.GetBO;
import cqie.personal.frame.entity.IndicatorInfo;
import cqie.personal.frame.exception.ValidationFailException;
import cqie.personal.frame.mapper.IndicatorInfoMapper;
import cqie.personal.frame.service.IndicatorInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cqie.personal.frame.vo.PageVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Yeeeeee
 * @since 2021-10-13
 */
@Service
public class IndicatorInfoServiceImpl extends ServiceImpl<IndicatorInfoMapper, IndicatorInfo> implements IndicatorInfoService {
    @Resource
    private IndicatorInfoMapper indicatorInfoMapper;

    @Override
    public PageVO<List<IndicatorInfo>> getIndicators(GetBO getBO) throws ValidationFailException {
        if(getBO==null){
            throw new ValidationFailException("服务器错误，未能获取请求参数");
        }

        QueryWrapper<IndicatorInfo> queryWrapper = new QueryWrapper<>();
        if(getBO.getId()!=null){
            queryWrapper.eq("id",getBO.getId());
            PageVO<List<IndicatorInfo>> unoData = new PageVO<>(indicatorInfoMapper.selectList(queryWrapper));
            unoData.setRecordCount(1);
            unoData.setTotalCount(1);
            return unoData;
        }
        if(getBO.getName()!=null){
            queryWrapper.like("indicator_name",getBO.getName());
        }
        int totalCount = indicatorInfoMapper.selectCount(queryWrapper);
        queryWrapper.last("LIMIT "+getBO.getPageOffSet()+","+getBO.getPageSize());
        List<IndicatorInfo> data = indicatorInfoMapper.selectList(queryWrapper);
        PageVO<List<IndicatorInfo>> result = new PageVO<>(data);
        result.setTotalCount(totalCount);
        result.setRecordCount(data.size());
        result.setPageNo(getBO.getPageNo());
        result.setPageSize(getBO.getPageSize());
        result.setTotalPage(
                totalCount%getBO.getPageSize()==0?
                        totalCount/getBO.getPageSize():
                        (totalCount/getBO.getPageSize())+1
                );
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_UNCOMMITTED)
    public synchronized boolean addNewIndicator(AddModifyBO addModifyBO) throws ValidationFailException {
        if(addModifyBO==null){
            throw new ValidationFailException("服务器错误，未能获取请求参数");
        }
        if(addModifyBO.getName()==null){
            throw new ValidationFailException("名称不可为空");
        }
        if(addModifyBO.getInfo()==null){
            throw new ValidationFailException("简介不能为空");
        }
        IndicatorInfo data = new IndicatorInfo();
        data.setIndicatorInfo(addModifyBO.getInfo());
        data.setIndicatorName(addModifyBO.getName());
        return indicatorInfoMapper.insert(data)==1;
    }
}
